﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace POS {
    public partial class Form1 : Form {
        PropertyPage pp = new PropertyPage();

        //create an Serial Port object
        SerialPort sp = new SerialPort("COM1");
        public Form1() {
            InitializeComponent();
        }

        private void Send(object sender, EventArgs e) {
            try {
                //write line to serial port
                sp.WriteLine(textBox.Text);
                //sp.Write();
                //clear the text box
                textBox.Text = "";
            } catch (System.Exception ex) {
                baudRatelLabel.Text = ex.Message;
            }
        }

        private void button3_Click(object sender, EventArgs e) {
            pp.ShowDialog();

            propertyButton.Hide();
        }

        private void button2_Click(object sender, EventArgs e) {
            try {
                //clear the text box
                textBox.Text = "";
                //read serial port and displayed the data in text box
                textBox.Text = sp.ReadLine();
            } catch (System.Exception ex) {
                baudRatelLabel.Text = ex.Message;
            }
        }

        private void button4_Click(object sender, EventArgs e) {
            //display values
            //if no property is set the default values
            if (pp.bRate == "" && pp.sBits == "") {
                dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();
                readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();
            } else {
                dataBitLabel.Text = "BaudRate = " + pp.bRate;
                readTimeOutLabel.Text = "StopBits = " + pp.sBits;
            }

            parityLabel.Text = "DataBits = " + sp.DataBits.ToString();
            stopBitLabel.Text = "Parity = " + sp.Parity.ToString();
            readTimeOutLabel.Text = "ReadTimeout = " +
                      sp.ReadTimeout.ToString();

            if (propertyButton.Visible == true)
                propertyButton.Hide();
            saveStatusButton.Hide();
            startCommButton.Show();

            try {
                //open serial port
                sp.Open();
                //set read time out to 500 ms
                sp.ReadTimeout = 500;
            } catch (System.Exception ex) {
                baudRatelLabel.Text = ex.Message;
            }
        }
    }
}
